<?php
/**
 * Created by jingjingxyk.
 * User: yaokun
 * Date: 17-7-18
 * Time: 下午11:51
 */

namespace ContentOutputBundle\Services;


class ContentOutputBookChapter
{
    private $em;

    public function __construct($em)
    {
        $this->em = $em;
    }

    /**
     * @param $book_id
     * @return mixed
     * 获得章节列表 无卷结构
     */
    public function getBookChapterList($book_id)
    {
        $num = $book_id % 8;
        $entity_name = "Chapter" . $num;
        $repository_name = "DBBundle:$entity_name";

        $repository = $this->em->getRepository($repository_name);

        $query = $repository->createQueryBuilder('p')
            ->where('p.bookId =:book_id')
            ->setParameter('book_id', $book_id)
            ->orderBy('p.orderNum', 'ASC')
            ->getQuery();
        return $query->getArrayResult();
    }

    /**
     * @param $book_id
     * @param $chapter_id
     * @return array
     * 获取单个章节信息
     */
    public function getBookChapter($book_id, $chapter_id)
    {
        $num = $book_id % 8;
        $entity_name = "Chapter" . $num;
        $repository_name = "DBBundle:$entity_name";

        $repository = $this->em->getRepository($repository_name);

        $query = $repository->createQueryBuilder('p')
            ->where('p.bookId =:book_id and p.chapterId=:chapter_id')
            ->setParameter('book_id', $book_id)
            ->setParameter('chapter_id', $chapter_id)
            ->getQuery();
        $chapter_obj = $query->getArrayResult();
        $chapter = [];
        if ($chapter_obj) {
            $chapter = $chapter_obj[0];
        }
        return $chapter;
    }

    /**
     * @param $book_id
     * @return mixed
     * 获得卷列表
     */
    public function getBookVolumeList($book_id)
    {
        //$num = $book_id % 8;
        $num = 0;
        $entity_name = "Volume" . $num;
        $repository_name = "DBBundle:$entity_name";

        $repository = $this->em->getRepository($repository_name);

        $query = $repository->createQueryBuilder('p')
            ->where('p.bookId =:book_id ')
            ->setParameter('book_id', $book_id)
            ->orderBy('p.orderNum', 'ASC')
            ->getQuery();
        $volumes= $query->getArrayResult();
        return $volumes;
    }

    /**
     * @param $book_id
     * 获得卷包含章节列表的数据
     */
    public function getVolumeAndChapterList($book_id)
    {
        $result["data"]=[];
        $result["code"] = 404;
        $result["msg"] = "no volume no chapter";

        $chapters = $this->getBookChapterList($book_id);

        $volume_list = $this->getBookVolumeList($book_id);

        if ($volume_list) {
            $temp_volume_data = [];
            $volume_id_list = [];
            $new_volume_list = [];
            //提取卷
            foreach ($chapters as $chapter) {
                $temp_volume_data[$chapter["volumeId"]][] = $chapter;
                if (!in_array($chapter["volumeId"], $volume_id_list)) {
                    $volume_id_list[] = $chapter["volumeId"];
                }
            }
            //去除无用的卷,创建新的卷 (卷不包含任何章节为无用的卷)
            foreach ($volume_list as $key => $volume) {
                if (in_array($volume["volumeId"], $volume_id_list)) {
                    $new_volume_list[] = $volume;
                }
            }
            foreach ($new_volume_list as $key => $new_volume) {
                $temp_new_volume["chapters"] = $temp_volume_data[$new_volume["volumeId"]];
                $temp_new_volume["volume_id"] = $new_volume["volumeId"];
                $temp_new_volume["volume_name"] = empty(trim($new_volume["volumeName"]))?("卷".($key+1)):trim($new_volume["volumeName"]);
                $temp_new_volume["volume_order_number"] = $key + 1;
                $result["data"][] = $temp_new_volume;
            }
            if(!empty($new_volume_list)){
                $result["code"] = 200;
                $result["msg"] = "success";
            }

        } else {//无卷 构造默认卷
            if(!empty($chapters)){
                $volume["volume_id"] = 0;
                $volume["volume_name"] = "正文卷";
                $volume["volume_order_number"] = 1;
                $volume["chapters"] = $chapters;

                $result["data"][] = $volume;
                $result["code"] = 200;
                $result["msg"] = "success";
            }

        }
        return $result;
    }

    /**
     * @param $book_id
     * @param $chapter_id
     * @return mixed
     * 获得章节内容
     */
    public function getBookChapterContent($book_id,$chapter_id)
    {
        $num = $book_id % 8;
        $entity_name = "ChapterContent" . $num;
        $repository_name = "DBBundle:$entity_name";

        $repository = $this->em->getRepository($repository_name);

        $query = $repository->createQueryBuilder('p')
            ->where('p.bookId =:book_id and p.chapterId=:chapter_id')
            ->setParameter('book_id', $book_id)
            ->setParameter('chapter_id', $chapter_id)
            ->getQuery();
        $chapter_content= $query->getArrayResult();
        if(empty($chapter_content)){
            return [];
        }else{
            return $chapter_content[0];
        }

    }
    public function __destruct()
    {
        unset($this->em);
    }

}